package com.kuaiying.kuai.ying.controller.printshop;


import java.io.File;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;
import com.kuaiying.kuai.ying.model.leaflet.LeafletModel;
import com.kuaiying.kuai.ying.model.print_file.PrintfileModel;
import com.kuaiying.kuai.ying.model.school.SchoolModel;
import com.kuaiying.kuai.ying.model.shop.ShopModel;
import com.kuaiying.kuai.ying.model.task_id.TaskidModel;
import com.kuaiying.kuai.ying.model.user.UserModel;
import com.kuaiying.kuai.ying.model.print_shop.PrintshopModel;
import com.kuaiying.kuai.ying.validator.HomeValidator;
import com.kuaiying.kuai.ying.validator.LoginValidator;
import com.kuaiying.kuai.ying.validator.RegistValidator;
import com.kuaiying.kuai.ying.validator.UploadValidator;

import java.text.SimpleDateFormat; 
import java.util.Date;

public class PrintshopController extends Controller{

	public void index(){
		String uName = getSessionAttr("printshop_phonenumber");
		if( uName != null ){
			redirect("/printshop/print");
			return;
		}
		render("login.html");
	}
	
	public void login(){
		String uName = getSessionAttr("printshop_phonenumber");
		if( uName != null ){
			System.out.println( "has printshop_phonenumber :" + uName );
			redirect("/printshop/print");
			return;
		}
		render("login.html");
	}
	
	@Before( LoginValidator.class )
	public void loginPost(){
		
		String phonenumber = getPara("phonenumber");
    	String pwd = getPara("password");
    	PrintshopModel printshop = PrintshopModel.me.getUserByNameAndPwd(phonenumber, pwd);
    	System.out.println( phonenumber+"	"+pwd );
    	if (printshop != null){
            String bbsID = phonenumber + pwd;
            setCookie("bbsID", bbsID, 3600*24*30);
            setSessionAttr("printshop_phonenumber", phonenumber);
            setSessionAttr("printshopID", printshop.get("print_shop_id"));
            Integer print_shop_id = printshop.get("print_shop_id");
            setSessionAttr("print_shop_id", print_shop_id);
            //修改用户登录时间
            PrintshopModel.me.upadtePrintshop_lastlogintime(print_shop_id);
            

            redirect("/printshop/print");
        }else{
            setAttr("loginError", "用户名或密码错误");
            render("login.html");
        }
	}

	public void unpdataPost(){
		Integer user_id = getSessionAttr("printshopID");
		PrintshopModel printshopModel = PrintshopModel.me.findById(user_id);
		String mobile = getPara("phonenum");
		String userName = getPara("truename");
		String email = getPara("email");
		String major = getPara("major");
		String password = getPara("password");
		String grade = getPara("grade");
		String school = getPara("school");
		printshopModel.set("email", email);
		printshopModel.set("school", school);
		if (password != null){
			printshopModel.set("password", password);
		}
		printshopModel.update();
		//System.out.println( PrintshopModel.me.upadteUser(user_id,userName,email,grade,major) );
		redirect("/printshop/info");
	}
	
	@Before( HomeValidator.class )
	public void home(){
		render("home.html");
	}
	
	public void print(){
		Integer printshopID = getSessionAttr("printshopID");
		if( printshopID != null ){
            //List<TaskidModel> list = TaskidModel.me.getAllTask_shop(printshopID);
            //setAttr("tasks", list);
		    //render("print.html");
    		Integer pageNumber = 1;
    		Integer pagevalue = 10;
    		try{
    			pageNumber = Integer.valueOf(getPara("page"));
    		}
    		catch (Exception e){
    			pageNumber = 1;
    		}
    		setAttr("nav_url", "/printshop/print");
    		List<TaskidModel> tasklist_tmp = TaskidModel.me.getAllTask_shop_finish_flag(printshopID,0);
    		Integer totlenum = tasklist_tmp.size();	//分页测试
            Integer totel_page = 1;
            if( totlenum%pagevalue == 0 ){
            	totel_page = (totlenum-totlenum%pagevalue)/pagevalue;
            }else{
            	totel_page = (totlenum-totlenum%pagevalue)/pagevalue+1;
            }
            List<TaskidModel> list_page = TaskidModel.me.getAllTask_shop_finish_flag_page(printshopID,0,pageNumber);
            setAttr("tasks", list_page);
            setAttr("pageNumber", pageNumber);
            setAttr("totalPage", totel_page);
            setAttr("totalRow", totlenum);
    		render("print.html");
		}
	}
	
	public void order(){
        Integer printshopID = getSessionAttr("printshopID");
        //修改用户登录时间
        if( printshopID != null ){
            //获取所以打印店
        	//List<TaskidModel> tasklist = TaskidModel.me.getAllTask_shop_finish_flag(printshopID,1);
            //setAttr("tasks", tasklist);
            //render("order.html");
        	//分页
    		Integer pageNumber = 1;
    		Integer pagevalue = 10;
    		try{
    			pageNumber = Integer.valueOf(getPara("page"));
    		}
    		catch (Exception e){
    			pageNumber = 1;
    		}
    		setAttr("nav_url", "/printshop/order");
    		List<TaskidModel> tasklist_tmp = TaskidModel.me.getAllTask_shop_finish_flag(printshopID,1);
    		Integer totlenum = tasklist_tmp.size();	//分页测试
            Integer totel_page = 1;
            if( totlenum%pagevalue == 0 ){
            	totel_page = (totlenum-totlenum%pagevalue)/pagevalue;
            }else{
            	totel_page = (totlenum-totlenum%pagevalue)/pagevalue+1;
            }
            List<TaskidModel> list_page = TaskidModel.me.getAllTask_shop_finish_flag_page(printshopID,1,pageNumber);
            setAttr("tasks", list_page);
            setAttr("pageNumber", pageNumber);
            setAttr("totalPage", totel_page);
            setAttr("totalRow", totlenum);
    		render("order.html");
        }
        else {
        	redirect("index");
        }
	}
	
	public void info(){
		Integer user_id = getSessionAttr("printshopID");
		if( user_id != null ){
			List<SchoolModel> schools = SchoolModel.me.getSchool();
			setAttr("schools", schools);
			PrintshopModel userModel = PrintshopModel.me.findById(user_id);
			setAttr("userModel", userModel);
		    render("info.html");
		}
	}
	
	public void quit(){
		removeSessionAttr("printshop_phonenumber");
		removeSessionAttr("printshopID");
		removeSessionAttr("print_shop_id");
		removeCookie("bbsID");
		render("login.html");
	}
	
	public void billing(){
		Integer printshopID = getSessionAttr("printshopID");
		if( printshopID != null ){
    		Integer pageNumber = 1;
    		Integer pagevalue = 10;
    		try{
    			pageNumber = Integer.valueOf(getPara("page"));
    		}
    		catch (Exception e){
    			pageNumber = 1;
    		}
    		setAttr("nav_url", "/printshop/billing");
			PrintshopModel printshopModel = PrintshopModel.me.findById(printshopID);
			setAttr("printshopModel", printshopModel);
			
        	List<TaskidModel> tasklist_tmp = TaskidModel.me.getAllTask_printShop(printshopID);
    		Integer totlenum = tasklist_tmp.size();	//分页测试
            Integer totel_page = 1;
            if( totlenum%pagevalue == 0 ){
            	totel_page = (totlenum-totlenum%pagevalue)/pagevalue;
            }else{
            	totel_page = (totlenum-totlenum%pagevalue)/pagevalue+1;
            }
            List<TaskidModel> tasklist = TaskidModel.me.getAllTask_printShop_page(printshopID,pageNumber);
            setAttr("tasks", tasklist);
            setAttr("pageNumber", pageNumber);
            setAttr("totalPage", totel_page);
            setAttr("totalRow", totlenum);
            
			render("billing.html");
		}
	}

	//public void download() {
	//    renderFile(new File("./WebRoot/upload/2011年计算机文化基础试题.pdf"));
	    //render("upload.html");
	//}
	
	public void download() {
		PrintfileModel printfile = PrintfileModel.me.findById(getParaToInt());
		String path = (String) printfile.get("path_new");
		System.out.println( path );
	    renderFile(new File(path));
	    //render("upload.html");
	}
	
	public void finish() {
        // 获取表单域名为studentID的值
        // Student.dao.deleteById(getPara("studentID"));
        // 获取url请求中第一个值
		//TaskidModel.me.deleteById(getParaToInt());
		Integer taskid = getParaToInt();
		TaskidModel.me.update_flag(taskid);
		//加金币
		Integer print_shop_id = getSessionAttr("printshopID");
		PrintshopModel printshop = PrintshopModel.me.findById(print_shop_id);
		TaskidModel task = TaskidModel.me.findById(taskid);
		printshop.set("all_gold", printshop.getInt("all_gold")+task.getInt("gold"));
		printshop.set("pend_gold", printshop.getInt("pend_gold")+task.getInt("gold"));
		printshop.update();
		
		List<TaskidModel> list = TaskidModel.me.getAllTask_shop(print_shop_id);
		setAttr("tasks", list);
		redirect("/printshop/print");
    }
	
	public void delete() {
        // 获取表单域名为studentID的值
        // Student.dao.deleteById(getPara("studentID"));
        // 获取url请求中第一个值
		TaskidModel.me.deleteById(getParaToInt());
		Integer print_shop_id = getSessionAttr("printshopID");
		List<TaskidModel> list = TaskidModel.me.getAllTask_shop(print_shop_id);
		setAttr("tasks", list);
		redirect("/printshop/order");
    }
	
}
